package cn.unknowpupil.controller.quartz;

import cn.unknowpupil.common.core.annotation.Log;
import cn.unknowpupil.common.core.enums.BusinessType;
import cn.unknowpupil.common.core.exceptions.TaskException;
import cn.unknowpupil.common.core.resopnse.ResultResponse;

import cn.unknowpupil.domain.system.SysJob;
import cn.unknowpupil.service.system.ISysJobService;
import cn.unknowpupil.util.CronUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created at 2020-11-06 by  w10g <br>
 * Initialize SysJob  ... <br>
 * 定时任务调度表 视图浏览层处理
 *
 * @author unknowpupil
 * @since 0.0.1
 */

@Api(tags = {"系统-定时任务调度表模块"}, value = "SysJob")
@RestController
@RequestMapping(value = "admin/sys-job")
public class SysJobController {
    @Autowired
    private  ISysJobService sysJobService;

    /**
     * 新增
     *
     * @param sysJob {@link SysJob}  定时任务调度表
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：新增定时任务调度表")
    @Log(title = "新增定时任务调度表", businessType = BusinessType.INSERT)
    @PostMapping("create")
    public ResultResponse create(@Validated @RequestBody SysJob sysJob) throws TaskException, SchedulerException {
        if (!CronUtils.isValid(sysJob.getCronExpression())) {
            return ResultResponse.buildFailure("cron表达式不正确");
        }
        // 业务逻辑
        return ResultResponse.buildSuccess(sysJobService.insertJob(sysJob));


    }

    /**
     * 修改
     *
     * @param sysJob {@link SysJob}  定时任务调度表
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：修改定时任务调度表")
    @Log(title = "修改定时任务调度表", businessType = BusinessType.UPDATE)
    @PutMapping("update")
    public ResultResponse update(@Validated @RequestBody SysJob sysJob) throws TaskException, SchedulerException {
        if (!CronUtils.isValid(sysJob.getCronExpression())) {
            return ResultResponse.buildFailure("cron表达式不正确");
        }
        // 业务逻辑
        return ResultResponse.buildSuccess(sysJobService.updateJob(sysJob));

    }

    /**
     * 获取
     *
     * @param id {@code Long}
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：获取定时任务调度表")
    @GetMapping("get/{id}")
    public ResultResponse get(@PathVariable("id") Long id) {
        SysJob sysJob = sysJobService.selectJobById(id);
        return ResultResponse.buildSuccess(sysJob);
    }

    /**
     * 删除
     *
     * @param ids {@code Long}
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：删除定时任务调度表")
    @Log(title = "删除定时任务调度表", businessType = BusinessType.DELETE)
    @DeleteMapping("remove/{ids}")
    public ResultResponse remove(@PathVariable("ids") Long[] ids) throws SchedulerException {
        // 业务逻辑
        sysJobService.deleteJobByIds(ids);
        return ResultResponse.success("删除成功");
    }

    /**
     * 分页
     *
     * @param current {@code int} 页码
     * @param size    {@code int} 笔数
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：分页定时任务调度表")
    @GetMapping("page/{current}/{size}")
    public ResultResponse<IPage<SysJob>> page(@PathVariable("current") int current, @PathVariable("size") int size, @ModelAttribute SysJob sysJob) {
        IPage<SysJob> page = sysJobService.page(current, size, sysJob);
        return ResultResponse.buildSuccess(page);
    }

    @ApiOperation("系统：定时任务状态修改")
    @Log(title = "定时任务状态修改", businessType = BusinessType.UPDATE)
    @PutMapping("/change/status")
    public ResultResponse changeStatus(@RequestBody SysJob job) throws SchedulerException, TaskException {

        SysJob newJob = sysJobService.selectJobById(job.getId());
        newJob.setStatus(job.getStatus());
        return ResultResponse.buildSuccess(sysJobService.changeStatus(newJob));

    }


    @ApiOperation("系统：定时任务立即执行一次")
    @Log(title = "定时任务立即执行一次", businessType = BusinessType.UPDATE)
    @PutMapping("/run")
    public ResultResponse run(@RequestBody SysJob job) throws SchedulerException, TaskException {
        sysJobService.run(job);
        return ResultResponse.success("立即执行一次");
    }
}
